/*
 * ChooseDeliveryHolder.java
 * business
 *
 * Created by ChenTao on 2017/2/14.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.ITag;
import com.yidiandao.business.data.bean.ShippingWayAdapter;
import com.yidiandao.business.ui.widget.listview.ViewHolder;
import com.yidiandao.business.ui.widget.view.RatioLayout;
import com.yidiandao.business.ui.widget.view.TagFlowLayout;
import com.yidiandao.business.ui.widget.view.TagView;
import com.yidiandao.business.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 确认订单-选择配送方式-Holder
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/14 10:26
 */
public class ChooseShippingWayHolder extends ViewHolder {

    private TextView tvShopName;
    private ImageView ivImage1;
    private RatioLayout ratioLayout2;
    private ImageView ivImage2;
    private RatioLayout ratioLayout3;
    private ImageView ivImage3;
    private RatioLayout ratioLayout4;
    private ImageView ivImage4;
    private TagFlowLayout tagFlowLayout;

    @Override
    protected void initView(View rootView) {
        tvShopName = (TextView) rootView.findViewById(R.id.tv_shop_name);
        ivImage1 = (ImageView) rootView.findViewById(R.id.iv_image_1);
        ratioLayout2 = (RatioLayout) rootView.findViewById(R.id.ratio_layout_2);
        ivImage2 = (ImageView) rootView.findViewById(R.id.iv_image_2);
        ratioLayout3 = (RatioLayout) rootView.findViewById(R.id.ratio_layout_3);
        ivImage3 = (ImageView) rootView.findViewById(R.id.iv_image_3);
        ratioLayout4 = (RatioLayout) rootView.findViewById(R.id.ratio_layout_4);
        ivImage4 = (ImageView) rootView.findViewById(R.id.iv_image_4);
        tagFlowLayout = (TagFlowLayout) rootView.findViewById(R.id.tag_flow_layout);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_choose_shipping_way;
    }

    public void refreshShippingWays(List<ShippingWayAdapter> shippingWays) {
        tagFlowLayout.setTagState(TagView.RECOMMEND);
        tagFlowLayout.setTagsData(parseShippingWay(shippingWays));
    }

    private List<ITag> parseShippingWay(List<ShippingWayAdapter> shippingWays) {
        List<ITag> tags = new ArrayList<>();
        for (ITag shippingWay : shippingWays) {
            tags.add(shippingWay);
        }
        return tags;
    }

    public void refreshTitle(String supplierName) {
        tvShopName.setText(supplierName);
    }

    public void refreshUIImages(List<String> goodsThumbs) {
        try {
            String imageUrl1 = goodsThumbs.get(0);
            ImageLoader.loadImage(mContext, ivImage1, imageUrl1);

            String imageUrl2 = goodsThumbs.get(1);
            ratioLayout2.setVisibility(View.VISIBLE);
            ImageLoader.loadImage(mContext, ivImage2, imageUrl2);

            String imageUrl3 = goodsThumbs.get(2);
            ratioLayout3.setVisibility(View.VISIBLE);
            ImageLoader.loadImage(mContext, ivImage3, imageUrl3);

            String imageUrl4 = goodsThumbs.get(3);
            ratioLayout4.setVisibility(View.VISIBLE);
            ImageLoader.loadImage(mContext, ivImage4, imageUrl4);
        } catch (IndexOutOfBoundsException e) {
            // 图片不足四张
        }
    }
}
